package com.hospital.demo.service.impl.dingyuxin;

import com.hospital.demo.mapper.dingyuxin.*;
import com.hospital.demo.model.dingyuxin.*;
import com.hospital.demo.service.dingyuxin.UserPaymentSer;
import com.hospital.demo.util.KeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserPaymentSerImpl implements UserPaymentSer {

    @Autowired
    private PaymentdetailMapper paymentdetailMapper;

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private PeopleMapper peopleMapper;

    @Autowired
    private MoneyMapper moneyMapper;

    @Autowired
    private ExpendMapper expendMapper;

    @Autowired
    private RefundMapper refundMapper;


    /**
     * 1.缴费处生成订单和订单详情
     */
    @Override
    public int buyDrugs(Payment payment, Paymentdetail paymentdetail) {
        int id = KeyUtils.getRandomInt();
        int money = discount(payment.getPeopleaccent(),payment.getTotalpay().intValue());
        payment.setPaymentid(id);
        paymentdetail.setPaymentid(id);
        payment.setTotalpay((double)money);
        paymentdetailMapper.insertSelective(paymentdetail);
        paymentMapper.insertSelective(payment);
        return 0;
    }

    /**
     * 2.根据医保减免相关费用
     */
    @Override
    public int discount(int peopleaccount, int totalmoney) {
        People people = peopleMapper.selectByPrimaryKey(peopleaccount);
        Money money = moneyMapper.selectByPrimaryKey(1);
        Expend expend = new Expend();
        if (people.getTotalmoney() >= totalmoney) {
            people.setTotalmoney(people.getTotalmoney() - totalmoney);
            peopleMapper.updateByPrimaryKeySelective(people);
            expend.setExpendmoney(totalmoney);
            expend.setInformation("医保支出");
            expend.setType("医保");
            money.setTotalmoney(money.getTotalmoney() - totalmoney);
            totalmoney = 0;
        } else {
            totalmoney -= people.getTotalmoney();
            expend.setExpendmoney(people.getTotalmoney());
            expend.setInformation("医保支出");
            expend.setType("医保");
            money.setTotalmoney(money.getTotalmoney() - people.getTotalmoney());
            people.setTotalmoney(0);
            peopleMapper.updateByPrimaryKeySelective(people);
        }
        moneyMapper.updateByPrimaryKey(money);
        expendMapper.insertSelective(expend);
        return totalmoney;
    }

    /**
     * 3.患者退货
     */
    @Override
    public int fundBack(int paymentid) {
        Money money = moneyMapper.selectByPrimaryKey(1);
        Payment payment = paymentMapper.selectByPrimaryKey(paymentid);
        money.setTotalmoney(money.getTotalmoney() - payment.getTotalpay().intValue());
        moneyMapper.updateByPrimaryKey(money);
        Refund refund = new Refund();
        refund.setPaymentid(paymentid);
        refund.setRefundid(KeyUtils.getRandomInt());
        refundMapper.insert(refund);
        paymentMapper.deleteByPrimaryKey(paymentid);
        return 0;
    }

    /**
     * 显示历史账单
     */
    @Override
    public List<Payment> paymentList(int peopleaccount) {
        return paymentMapper.selectBypeopleaccount(peopleaccount);
    }

    /**
     * 显示医保余额
     */
    @Override
    public int balance(int peopleaccount) {
        return peopleMapper.selectByPrimaryKey(peopleaccount).getTotalmoney();
    }
}
